package com.skyworth.web.admin.dishware.service.impl;

import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.skyworth.web.admin.dishware.domain.DishwareModel;
import com.skyworth.web.admin.dishware.form.DishwareForm;
import com.skyworth.web.admin.dishware.repository.DishwareRepository;
import com.skyworth.web.admin.dishware.service.DishwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by skyworth on 2016/9/20.
 */
@Service
public class DishwareServiceImpl implements DishwareService{
	@Autowired
	private DishwareRepository dishwareRepository;

	@Override
	public DishwareModel addDishware(DishwareForm dishwareForm) {
		DishwareModel dishwareModel = new DishwareModel();
		dishwareModel.setName(dishwareForm.getName());
		dishwareModel.setAutoAdd(dishwareForm.getAutoAdd());
		dishwareModel.setUnit(dishwareForm.getUnit());
		dishwareModel.setUnitPrice(dishwareForm.getUnitPrice());
		dishwareModel.setStoreId(dishwareForm.getStoreId());
		dishwareRepository.save(dishwareModel);
		return dishwareModel;
	}

	@Override
	public void delDishware(long id) {
		dishwareRepository.delete(id);
	}

	@Override
	public DishwareModel updateDishware(DishwareForm dishwareForm) {
		DishwareModel dishwareModel = this.findDishware(dishwareForm.getId());
		dishwareModel.setName(dishwareForm.getName());
		dishwareModel.setAutoAdd(dishwareForm.getAutoAdd());
		dishwareModel.setUnit(dishwareForm.getUnit());
		dishwareModel.setUnitPrice(dishwareForm.getUnitPrice());
		dishwareModel.setStoreId(dishwareForm.getStoreId());
		dishwareRepository.save(dishwareModel);
		return dishwareModel;
	}

	@Override
	public DishwareModel findDishware(long id) {
		return dishwareRepository.findOne(id);
	}


	@Override
	public Page<DishwareModel> pageDishware(long store_id, Pageable pageable) {
		return dishwareRepository.allByStoreId(store_id,pageable);
	}

	@Override
	public boolean existDishwareByName(String name, long store_id) {
		return dishwareRepository.existByName(name, store_id);
	}

	@Override
	public boolean existDishware(String name, long id, long store_id) {
		return dishwareRepository.existDishware(name,id, store_id);
	}

}
